package flc.ast.activity;

import androidx.annotation.NonNull;

import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SkillAdapter;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityProfessionalSkillBinding;
import flc.ast.dialog.AddSkillDialog;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.ResumeDataUtils;

public class ProfessionalSkillActivity extends BaseAc<ActivityProfessionalSkillBinding> {

    public static int position;
    public static int modulePos;
    private ResumeBean resumeBean;
    private ResumeBean.Module module;
    private ResumeBean.SkillData skillData;
    private SkillAdapter skillAdapter;
    private List<ResumeBean.DetailItem> listSkill = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_professional_skill;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProfessionalSkillBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(mContext);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvSkillList.setLayoutManager(layoutManager);
        skillAdapter = new SkillAdapter();
        mDataBinding.rvSkillList.setAdapter(skillAdapter);
        skillAdapter.addChildClickViewIds(R.id.ivSkillItemDelete);
        skillAdapter.addChildClickViewIds(R.id.ivSkillItemAdd);
        skillAdapter.setOnItemClickListener(this);
        skillAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                save();
                break;
            case R.id.tvTitle://标题
                showRenameDialog();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        int id = view.getId();
        if (id == R.id.ivSkillItemDelete) {//删除
            listSkill.remove(position);
            skillAdapter.setList(listSkill);
        } else if (id == R.id.ivSkillItemAdd) {//添加
            showAddSkillDialog();
        }
    }

    @Override
    protected void initData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        if (module.getData() != null) {
            skillData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.SkillData.class);
            if (skillData.getDetailList() != null) {
                skillData.setDetailList(GsonUtils.fromJson(skillData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                }.getType()));
                listSkill.addAll(skillData.getDetailList());
            }
        } else {
            skillData = new ResumeBean.SkillData();
        }
        listSkill.add(new ResumeBean.DetailItem(""));
        skillAdapter.setList(listSkill);
    }

    private void save() {
        if (listSkill.size() == 1) {
            ToastUtils.showShort(getString(R.string.please_add_professional_skill));
            return;
        }
        listSkill.remove(listSkill.size() - 1);
        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        skillData.setDetailList(listSkill);
        skillData.setSkillList(new ArrayList<>());
        module.setData(skillData);
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }

    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.text = mDataBinding.tvTitle.getText().toString().trim();
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }

    private void showAddSkillDialog() {
        AddSkillDialog addSkillDialog = new AddSkillDialog(mContext);
        addSkillDialog.setListener(new AddSkillDialog.Ilistener() {

            @Override
            public void clickRename(String skill) {
                listSkill.add(listSkill.size() - 1, new ResumeBean.DetailItem(skill));
                skillAdapter.setList(listSkill);
            }
        });
        addSkillDialog.show();
    }

}